package com.example.dryyzx.dao;

import com.example.dryyzx.encapsulation.LeavePageEncapsulation;
import com.example.dryyzx.entity.Checkout;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface CheckoutMapper {
    @Delete({
        "delete from checkout",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into checkout (id, customer_id, ",
        "type, reason, checkout_time, ",
        "apply_time, approval_time, approver, ",
        "leave_status, remarks, enable)",
        "values (#{id,jdbcType=INTEGER}, #{customerId,jdbcType=INTEGER}, ",
        "#{type,jdbcType=CHAR}, #{reason,jdbcType=VARCHAR}, #{checkoutTime,jdbcType=TIMESTAMP}, ",
        "#{applyTime,jdbcType=TIMESTAMP}, #{approvalTime,jdbcType=TIMESTAMP}, #{approver,jdbcType=VARCHAR}, ",
        "#{leaveStatus,jdbcType=CHAR}, #{remarks,jdbcType=VARCHAR}, #{enable,jdbcType=BIT})"
    })
    int insert(Checkout record);

    @InsertProvider(type=CheckoutSqlProvider.class, method="insertSelective")
    int insertSelective(Checkout record);

    @Select({
        "select",
        "id, customer_id, ",
        "type, reason, checkout_time, ",
        "apply_time, approval_time, approver, ",
        "leave_status, remarks, enable",
        "from checkout",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="customer_id", property="customerId", jdbcType=JdbcType.INTEGER),
        @Result(column="type", property="type", jdbcType=JdbcType.CHAR),
        @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
        @Result(column="checkout_time", property="checkoutTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="apply_time", property="applyTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="approval_time", property="approvalTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="approver", property="approver", jdbcType=JdbcType.VARCHAR),
        @Result(column="leave_status", property="leaveStatus", jdbcType=JdbcType.CHAR),
        @Result(column="remarks", property="remarks", jdbcType=JdbcType.VARCHAR),
        @Result(column="enable", property="enable", jdbcType=JdbcType.BIT)
    })
    Checkout selectByPrimaryKey(Integer id);

    @Select({
            "select",
            "id, customer_id, ",
            "type, reason, checkout_time, ",
            "apply_time, approval_time, approver, ",
            "leave_status, remarks, enable",
            "from checkout",
            "where id = #{id,jdbcType=INTEGER} and enable = 1"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="customer_id", property="customerId", jdbcType=JdbcType.INTEGER),
            @Result(column="type", property="type", jdbcType=JdbcType.CHAR),
            @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
            @Result(column="checkout_time", property="checkoutTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="apply_time", property="applyTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="approval_time", property="approvalTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="approver", property="approver", jdbcType=JdbcType.VARCHAR),
            @Result(column="leave_status", property="leaveStatus", jdbcType=JdbcType.CHAR),
            @Result(column="remarks", property="remarks", jdbcType=JdbcType.VARCHAR),
            @Result(column="enable", property="enable", jdbcType=JdbcType.BIT)
    })
    Checkout selectByIdEnable(@Param("id") Integer id);

    @Select({
            "select",
            "checkout.id, customer.name, customer.sex, customer.age, customer.archivesId, customer.checkin_time, checkout.checkout_time, ",
            "checkout.type, checkout.reason, checkout.leave_status, ",
            "checkout.apply_time, checkout.approval_time, checkout.approver, ",
            "checkout.remarks ",
            "from checkout,customer",
            "where customer.id=checkout.customer_id and customer.name like concat('%',#{name,jdbcType=VARCHAR},'%') ",
            "and checkout.enable=1"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="sex", property="gender", jdbcType=JdbcType.CHAR),
            @Result(column="age", property="age", jdbcType=JdbcType.INTEGER),
            @Result(column="archivesId", property="file_number", jdbcType=JdbcType.CHAR),
            @Result(column="checkin_time", property="checkin_date", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="checkout_time", property="leave_date", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="type", property="type", jdbcType=JdbcType.CHAR),
            @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
            @Result(column="leave_status", property="leave_status", jdbcType=JdbcType.CHAR),
            @Result(column="apply_time", property="apply_date", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="approval_time", property="approval_date", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="approver", property="approver", jdbcType=JdbcType.VARCHAR),
            @Result(column="remarks", property="remark", jdbcType=JdbcType.VARCHAR),
    })
    List<LeavePageEncapsulation> selectCheckoutCustomerByName(String name);

    @Select({
            "select",
            "checkout.id, customer.name, customer.sex, customer.age, customer.archivesId, customer.checkin_time, checkout.checkout_time, ",
            "checkout.type, checkout.reason, checkout.leave_status, ",
            "checkout.apply_time, checkout.approval_time, checkout.approver, ",
            "checkout.remarks ",
            "from checkout,customer",
            "where customer.id=checkout.customer_id and customer.name like concat('%',#{name,jdbcType=VARCHAR},'%') ",
            "and checkout.enable=1 and checkout.leave_status='未审核'"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="sex", property="gender", jdbcType=JdbcType.CHAR),
            @Result(column="age", property="age", jdbcType=JdbcType.INTEGER),
            @Result(column="archivesId", property="file_number", jdbcType=JdbcType.CHAR),
            @Result(column="checkin_time", property="checkin_date", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="checkout_time", property="leave_date", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="type", property="type", jdbcType=JdbcType.CHAR),
            @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
            @Result(column="leave_status", property="leave_status", jdbcType=JdbcType.CHAR),
            @Result(column="apply_time", property="apply_date", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="approval_time", property="approval_date", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="approver", property="approver", jdbcType=JdbcType.VARCHAR),
            @Result(column="remarks", property="remark", jdbcType=JdbcType.VARCHAR),
    })
    List<LeavePageEncapsulation> selectUnapprovedCheckoutCustomerByName(String name);

    @UpdateProvider(type=CheckoutSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Checkout record);

    @Update({
        "update checkout",
        "set customer_id = #{customerId,jdbcType=INTEGER},",
          "type = #{type,jdbcType=CHAR},",
          "reason = #{reason,jdbcType=VARCHAR},",
          "checkout_time = #{checkoutTime,jdbcType=TIMESTAMP},",
          "apply_time = #{applyTime,jdbcType=TIMESTAMP},",
          "approval_time = #{approvalTime,jdbcType=TIMESTAMP},",
          "approver = #{approver,jdbcType=VARCHAR},",
          "leave_status = #{leaveStatus,jdbcType=CHAR},",
          "remarks = #{remarks,jdbcType=VARCHAR},",
          "enable = #{enable,jdbcType=BIT}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Checkout record);
}